package cn.display.pixelworksx7
// import cn.display.pixelworksx7.service.FloatWindowService
import android.Manifest
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Vibrator
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.PermissionChecker
import androidx.viewpager.widget.ViewPager
import cn.display.pixelworksx7.constant.GlobalData
import cn.display.pixelworksx7.databinding.ActivityMainBinding
//import cn.display.pixelworksx7.service.HealthService
import cn.display.pixelworksx7.threadpool.SystemThreadPool
import cn.display.pixelworksx7.ui.main.SectionsPagerAdapter
import cn.display.pixelworksx7.util.IrisConfigUtil
import cn.display.pixelworksx7.util.ShellUtil
import com.google.android.material.tabs.TabLayout


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val TAG = "MainActivity"
    var notificationManager: NotificationManager? = null
    override fun onResume() {
        super.onResume()
        // 判断是否给予权限
        checkPermission() // 检测权限
    }

    private fun checkPermission() {
        var thread = Thread(Runnable {
            kotlin.run {
                if (PermissionChecker.checkPermission(
                        this,
                        Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                        0,
                        0,
                        this.packageName
                    ) == -2
                ) {
                    var intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    intent.setData(Uri.parse("package:" + this.getPackageName()));
                    startActivity(intent);
                }
            }
        })
        thread.start()
        thread.join()
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
        val viewPager: ViewPager = binding.viewPager
        viewPager.adapter = sectionsPagerAdapter
        val tabs: TabLayout = binding.tabs
        tabs.setupWithViewPager(viewPager)
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            if (!GlobalData.isExistsIrisConfigBin && !GlobalData.isExistsTouchConfigBin) {
                ShellUtil.toast(this, "当前设备不存在独显芯片/不是黑厂新思触控ic")
            } else {
                checkPermission()
//                val intent = Intent()
//                val packageName = "com.xiaomi.macro" // 目标应用的包名
//
//                val className = "com.xiaomi.macro.MainService" // 目标应用的Service类全名
//
//                intent.setClassName(packageName, className)
//                startService(intent)
//                val intent = Intent(this, HealthService::class.java)
//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//                    startForegroundService(intent)
//                } else {
//                    startService(intent)
//                }
                GlobalData.vibrator = this.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
                ShellUtil.get_root(this)
                GlobalData.storageDir = this.baseContext.filesDir
                SystemThreadPool.getExecutorService().execute(Runnable {
                    var pixelworksServiceName =
                        "cn.display.pixelworksx7/cn.display.pixelworksx7.listener.TopAppListener"
                    var execCommand = ShellUtil.execCommand("settings get secure enabled_accessibility_services", true)
                    execCommand.successMsg
                    ShellUtil.execCommand(
                        "settings put secure enabled_accessibility_services " + execCommand.successMsg + ":" + pixelworksServiceName,
                        true
                    )
                    ShellUtil.execCommand("settings put secure accessibility_enabled 1", true)
                })
                SystemThreadPool.getExecutorService().execute(Runnable {
                    IrisConfigUtil.loadIrisConfigFileParseToMap()
                })
            }
    }
}
